package com.day.newcorp;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;

import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * The Class WebComponentsConfig. This class reads the properties from the Felix
 * Console.
 * 
 * @scr.component immediate="true"
 *                label="CCPortal 2.0 | CCP Auth Config Property Reader Utility"
 *                description
 *                ="CCPortal 2.0 | CCP Auth Config Property Reader Utility"
 *                name="com.day.newcorp.CCPAuthConfig"
 */
public class CCPAuthConfig {
	private static final Logger logger = LoggerFactory.getLogger(CCPAuthConfig.class);	
	
	/** @scr.property name="ccp.defaultUserName" description="Default User Name" value="defaultuser@newcorp.com"*/
	public static String defaultUserName;
	
	/** @scr.property name="ccp.defaultUserPassword" description="Default User password" value=""*/
	public static String defaultUserPassword;
	
	
	/** @scr.property name="ccp.auth.url.fileExtension" description="The URL file name extensions that need to be validated like html " cardinality=50 */
	public static String[] ccpAuthExtensions;
	
	

	/** @scr.property name="ccp.portal.sessionTimeout" description="Session Timeout in mins" cardinality=200 */
	public static Map<String,String> ccpPortalSessionTimeout = new HashMap<String,String>();
	
	/** @scr.property name="ccp.auth.createSecureCookie" description="create ccp_ cookie as a secure cookie" value="false" type="Boolean"*/
	public static boolean createSecureCookie;
		
	
	/**
	 * Activate.
	 *
	 * @param context the context
	 * @throws Exception the exception
	 */
	@SuppressWarnings("unchecked")
	protected void activate(final ComponentContext context) throws Exception {
		try {
			logger.debug("*************** CCP Auth Config  Property Reader [START] ***************");
			
			final Dictionary<String, ?> props = context.getProperties();		
			
			defaultUserName = OsgiUtil.toString(props.get("ccp.defaultUserName"), "");
			logger.debug("[[[defaultUserName=" + defaultUserName + "]]]");
			
			createSecureCookie = OsgiUtil.toBoolean(props.get("ccp.auth.createSecureCookie"), false);
			logger.debug("[[[createSecureCookie=" + createSecureCookie + "]]]");		
			
			defaultUserPassword = OsgiUtil.toString(props.get("ccp.defaultUserPassword") , "");	
			logger.debug("[[[defaultUserPassword=HIDDEN]]]");
			ccpAuthExtensions = OsgiUtil.toStringArray(props.get("ccp.auth.url.fileExtension") , new String[]{"html"});	
			logger.debug("[[[ccpAuthExtensions=");
			for(String ext: ccpAuthExtensions) {
				logger.debug(ext);
			}
			logger.debug("]]]");
						
			String[] sessionTimeout = OsgiUtil.toStringArray(props.get("ccp.portal.sessionTimeout") , new String[]{"portalname:30"});	
			logger.debug("[[[sessionTimeout=");
			for(String ext: sessionTimeout) {
				logger.debug(ext);
				String[] parts = ext.split(":");
				if(parts.length==2){
				    ccpPortalSessionTimeout.put(parts[0],parts[1]);
				}
			}
			logger.debug("]]]");
			
			
			logger.debug("***************CCP Auth Config  Property Reader Loading Property [END] ***************");
		} catch (final Exception e) {
			logger.error("Error while trying to  read CCP Auth Config  Property Reader : " + e.getMessage());
			throw e;
		}
	}

	/**
	 * Deactivate.
	 *
	 * @param context the context
	 */
	protected void deactivate(final ComponentContext context) {
	}
}
